package xin.yi.job;

import com.offcn.redis.RedisConstant;
import com.offcn.utils.MessageConstant;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Set;

/**
 * @program: offcnpe_parent
 * @description:
 * @author: xin yi
 * @create: 2021-07-19 10:41
 */
@Component/*交给spring容器管理*/
public class CleanImgJob{

    @Autowired/**/
    private StringRedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 3 * * ?")/*设置每天三点执行*/
    public void cleanJobTask(){
        Set<String> difference = redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC_UPLOAD, RedisConstant.SETMEAL_PIC_DB);
        if (difference.size() > 0){
            for (String s : difference) {
                /*从图片的硬盘位置移除*/
                /*删除指定位置的本地图片（删除成功返回true）*/
                boolean boo = new File("F:\\setmealpic\\uploadpic\\" + s).delete();
                if (boo){
                    System.out.println(MessageConstant.DELETE_LOCALPIC_SUCCESS);
                }else {
                    System.out.println(MessageConstant.DELETE_LOCALPIC_FAIL);
                }
                /*再从redis位置移除*/
                redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_UPLOAD).remove(s);

            }
        }


    }

}
